package cn.aitstone.controller;

import cn.aitstone.dto.ProductDto;
import cn.aitstone.entity.Product;
import cn.aitstone.entity.Store;
import cn.aitstone.feign.StoreFeignClient;
import cn.aitstone.service.IProductService;
import cn.aitstone.utils.AjaxResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class ProductFeignController {
    @Autowired
    private StoreFeignClient storeFeignClient;

    @Autowired
    private IProductService productService;

    @PostMapping("/product/add")
    public AjaxResult add(@RequestBody ProductDto productDto){
        Product product = new Product();
        BeanUtils.copyProperties(productDto, product);
        productService.add(product);
        Store store = new Store();
        store.setProductId(product.getId());
        store.setNumber(productDto.getNumber());
        AjaxResult ajaxResult = storeFeignClient.add(store);
        if (!ajaxResult.getSuccess()){

            return AjaxResult.me().setSuccess(false).setMessage("降级了");
        }
        return AjaxResult.me();
    }


}
